﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Windows;

namespace OnlineBookReader
{
    /// <summary>
    /// The SampleBrowserItem class represents an individual sample in tab control
    /// samples. 
    /// </summary>
    public class SampleBrowserItem
    {
        /// <summary>
        /// Gets the name of the sample.
        /// </summary>
        public string Name { get; private set; }
        public string ViewName { get; private set; }
        /// <summary>
        /// Gets or sets the name of the sample to use for sorting.
        /// </summary>
        private string OriginalName { get; set; }
        public string Uri { get; private set; }

        /// <summary>
        /// Gets the sample type.
        /// </summary>
        public Type SampleType { get; private set; }

        //public SampleTreeItem TreeItem { get; private set; }

  
        private SampleBrowserItem(string name,string uri, Lazy<FrameworkElement, ISampleMetadata> lazySample)
        {
            Name = name;
            SampleType = lazySample.Value.GetType();
            OriginalName = lazySample.Metadata.Name;
            ViewName = lazySample.Metadata.ViewName;
            Uri = uri;
          //  TreeItem = treeItem;
        }
        /// <summary>
        /// Overrides the ToString method to return the Name of the Sample.
        /// </summary>
        /// <returns>Returns the Name of the sample.</returns>
        public override string ToString()
        {
            return  Name;
        }

        /// <summary>
        /// Gets the sample represented by this item.
        /// </summary>
        /// <returns>The sample represented by this item.</returns>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Creates a new instance each time it is accessed.")]
        public FrameworkElement GetSample()
        {
            // Create the sample from the type if we have one
           
                FrameworkElement sample =string.IsNullOrEmpty(Uri)?(FrameworkElement)Activator.CreateInstance(this.SampleType):(FrameworkElement)Activator.CreateInstance(this.SampleType, Uri);
            sample.VerticalAlignment = VerticalAlignment.Top;
            sample.HorizontalAlignment = HorizontalAlignment.Left;
            sample.Margin = new Thickness(10, 0, 0, 0);
            return sample;
        }
        internal static void AddSample(IList<SampleBrowserItem> Samples, Lazy<FrameworkElement, ISampleMetadata> sample,string uri)
        {
           
            string name = sample.Metadata.Name;
            // Find any existing item
            SampleBrowserItem item = null;
           
            // Escape any name sorting
            string escapedName = name;
            if (name[0] == '(')
            {
                int index = name.IndexOf(')');
                if (index > 0)
                {
                    escapedName = name.Substring(index + 1, name.Length - index - 1);
                }
            }

            item = new SampleBrowserItem(escapedName, uri,sample);

           
            Samples.Add(item);
            if(Samples.Count!=1)

                throw new Exception();
        }
     
    }
}